﻿using CPPEI.Coolzon.Designer.UI;
using CPPEI.Coolzon.Designer.UI.Editor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPPEI.Coolzon.Designer.Event.UI;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.Common;
using Xceed.Wpf.Toolkit.PropertyGrid;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for PropertyView.xaml
    /// </summary>
    public partial class PropertyView : View
    {
        public PropertyView()
        {
            InitializeComponent();
        }

        [Subscribe]
        public void UIPropertyValueChangedEvent(Coolzon.UI.Designtime.EditorValueChangedEvent @event)
        {
            Part partInstance = @event.PropertyItem.Instance as Part;
            object newValue = @event.NewValue;
            object oldValue = @event.PropertyItem.Value;
            Coolzon.UI.XProperty.XPropertyValueEditor editor = @event.PropertyItem.Editor as Coolzon.UI.XProperty.XPropertyValueEditor;
            if (UIContext.Current.IsEditTemplateMode)
            {
                if(@event.PropertyObject != null)
                {
                    editor.SetValue(@event.PropertyObject, newValue);
                }
                try
                {
                    Coolzon.UI.XProperty.XPropertyRTUtil.SetPropertyValue(partInstance.Metadata, @event.PropertyItem.Description, @event.PropertyItem.PropertyDescriptor.PropertyType, newValue);
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTAG, "模板-编辑器{0}更新组件{1}的属性{2}出错,ex:{3}", editor.GetType().FullName, partInstance.Name, @event.PropertyItem.PropertyDescriptor.Name, ex);
                }
                return;
            }
            if (UIContext.Current.CurrentDesignView.API_IsEditable())
            {
                if (partInstance is Coolzon.UI.AreaPart && partInstance.ParentPart is Coolzon.UI.ScenePart && (partInstance.ParentPart as Coolzon.UI.ScenePart).SceneMatadata.IsAllowSwapArea)
                {
                    if (string.Equals(@event.PropertyItem.Description, "Left", StringComparison.CurrentCultureIgnoreCase))
                    {
                        //最左侧区域不可改变left
                        if ((partInstance.ParentPart as Coolzon.UI.ScenePart).GetLeftRootArea(partInstance as AreaPart) == partInstance)
                        {
                            editor.UnDo(oldValue);
                        }
                        else
                        {
                            AreasInSceneResizeOperation operation = new AreasInSceneResizeOperation(UIContext.Current.CurrentDesignView, partInstance.ParentPart as Coolzon.UI.ScenePart, partInstance as Coolzon.UI.AreaPart, (double)newValue, partInstance.XWidth);
                            OperationCenter.Instance.Do(operation);
                        }
                    }
                    else if (string.Equals(@event.PropertyItem.Description, "Width", StringComparison.CurrentCultureIgnoreCase))
                    {
                        AreasInSceneResizeOperation operation = new AreasInSceneResizeOperation(UIContext.Current.CurrentDesignView, partInstance.ParentPart as Coolzon.UI.ScenePart, partInstance as Coolzon.UI.AreaPart, partInstance.XLeft, (double)newValue);
                        OperationCenter.Instance.Do(operation);
                    }
                    else if (string.Equals(@event.PropertyItem.Description, "Top", StringComparison.CurrentCultureIgnoreCase) || string.Equals(@event.PropertyItem.Description, "Height", StringComparison.CurrentCultureIgnoreCase))
                    {
                        editor.UnDo(oldValue);
                    }
                    else
                    {
                        ChangePropertyOperation operation = new ChangePropertyOperation(editor, @event.PropertyItem.Description, @event.PropertyObject, @event.PropertyItem.PropertyDescriptor.PropertyType, oldValue, newValue);
                        OperationCenter.Instance.Do(operation);
                    }
                }
                else if (partInstance is Coolzon.UI.ScenePart)
                {
                    if (string.Equals(@event.PropertyItem.Description, "Left", StringComparison.CurrentCultureIgnoreCase) || string.Equals(@event.PropertyItem.Description, "Top", StringComparison.CurrentCultureIgnoreCase))
                    {
                        editor.UnDo(oldValue);
                    }
                    else
                    {
                        ChangePropertyOperation operation = new ChangePropertyOperation(editor, @event.PropertyItem.Description, @event.PropertyObject, @event.PropertyItem.PropertyDescriptor.PropertyType, oldValue, newValue);
                        OperationCenter.Instance.Do(operation);
                    }
                }
                else
                {
                    ChangePropertyOperation operation = new ChangePropertyOperation(editor, @event.PropertyItem.Description, @event.PropertyObject, @event.PropertyItem.PropertyDescriptor.PropertyType, oldValue, newValue);
                    OperationCenter.Instance.Do(operation);
                }
            }
            else
            {
                editor.UnDo(oldValue);
            }
        }

        public void SelectPartChanged(Coolzon.UI.Part partInstance)
        {
            InitProperties(partInstance);
        }

        public void Clear()
        {
            _propertyGrid.SelectedObject = null;
        }
        
        private void InitProperties(Coolzon.UI.Part partInstance)
        {
            if(partInstance == null)
            {
                return;
            }
            PropertyDefinition pd = new PropertyDefinition();

            List<Coolzon.UI.XProperty.XPropertyDescription> propertyList = CPPEI.Coolzon.UI.XProperty.XPropertyDescriptionCache.Instance.Get(partInstance.GetType());
            foreach (var item in propertyList)
            {
                pd.TargetProperties.Add(item.PropertyInfo.Name);
            }
            _propertyGrid.PropertyDefinitions.Clear();
            _propertyGrid.PropertyDefinitions.Add(pd);
            _propertyGrid.SelectedObject = partInstance;
        }
    }
}
